package com.fzy.test.leetcode464;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Main test = new Main();
        test.minLength("ABFCACDB");
        test.makeSmallestPalindrome("abcd");
    }

    public int minLength(String s) {
        Deque<Character> stack = new ArrayDeque<>();
        int n = s.length();
        for(int i = 0;i<n;i++){
            if(stack.isEmpty()){
                stack.push(s.charAt(i));
            }else {
                if(stack.peek() == 'A' && s.charAt(i) == 'B'){
                    stack.pop();
                    continue;
                }
                if(stack.peek() == 'C' && s.charAt(i) == 'D'){
                    stack.pop();
                    continue;
                }
                stack.push(s.charAt(i));
            }
        }
        return stack.size();
    }

    public String makeSmallestPalindrome(String s) {

        int len = s.length();
        char[] str = s.toCharArray();
        int left = 0;
        int right = len-1;
        while (left<right){
            if(s.charAt(left) != s.charAt(right)){
                if(s.charAt(left) < s.charAt(right)){
                    str[right] = s.charAt(left);
                }else {
                    str[left] = s.charAt(right);
                }
            }
            left++;
            right--;
        }
        return String.valueOf(str);
    }


    public int punishmentNumber(int n) {
        int ans = 0;
        int[] arr = new int[n+1];
        for(int i = 1;i<=n;i++){
            arr[i] = i*i;
        }
        for(int i = 1;i<=n;i++){
            if(isTrue(arr[i],i)){
                ans += arr[i];
            }
        }

        return ans;
    }

    private boolean isTrue(int num,int val){
        String str = String.valueOf(num);

        char[] chars = str.toCharArray();

        for(int i = 0;i<chars.length;i++){
            for(int j = i;j<chars.length;j++){
                
            }
        }

        return true;
    }
}
